package com.letao.controller;

import com.letao.pojo.TbItemCat;
import com.letao.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ac on 2017/1/10.
 */
@Controller
@RequestMapping("/item/cat")
public class ItemCatController {
    @Autowired
    private ItemCatService itemCatService;

    @RequestMapping("/list")
    @ResponseBody
    public List categoryList(@RequestParam(value = "id", defaultValue = "0") Long parentID){
        List<TbItemCat> catList = itemCatService.getItemCatListByParent(parentID);
        List list = new ArrayList();
        //构建树节点
        for (TbItemCat cat :
                catList) {
            Map node = new HashMap();
            node.put("id", cat.getId());
            node.put("text", cat.getName());
            node.put("state", cat.getIsParent() ? "closed" : "open");
            list.add(node);
        }

        return list;
    }
}
